package recyclerview.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lib.recyclyeviewmodule.R;

import recyclerview.swipetoloadlayout.SwipeRefreshTrigger;
import recyclerview.swipetoloadlayout.SwipeTrigger;


/**
 * Created by Administrator on 2016/7/27.
 */

public class Header extends FrameLayout implements SwipeTrigger,SwipeRefreshTrigger {
    int mHeaderHeight;
    Animation down;
    Animation up;
    TextView info;
    TextView time;
    ImageView arrow;
    ProgressBar pb;
    boolean rotated;
    public Header(Context context) {
        this(context,null);
    }

    public Header(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public Header(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        String lastTime = new PreferenceUtils("Pull_List").getString(
//                "last_refresh_time_"+lab, "");
        mHeaderHeight = getResources().getDimensionPixelOffset(R.dimen.header_height);
        down= AnimationUtils.loadAnimation(context,R.anim.rotate_down);
        up= AnimationUtils.loadAnimation(context,R.anim.rotate_up);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
       info= (TextView) findViewById(R.id.info);
       time= (TextView) findViewById(R.id.time);
       arrow= (ImageView) findViewById(R.id.arrow);
       pb= (ProgressBar) findViewById(R.id.progressbar);
    }

    /**
     * 刷新的时候
     */
    @Override
    public void onRefresh() {
        info.setText("加载中...");
        arrow.clearAnimation();
        arrow.setVisibility(INVISIBLE);
        pb.setVisibility(VISIBLE);
    }

    @Override
    public void onPrepare() {

    }

    /**
     * 拖拽时候
     * @param y 偏移量
     * @param isComplete
     * @param automatic
     */
    @Override
    public void onMove(int y, boolean isComplete, boolean automatic) {
        if (!isComplete) {
            arrow.setVisibility(VISIBLE);
            pb.setVisibility(GONE);
            if (y > mHeaderHeight) {
                info.setText("松开可以刷新");
                if (!rotated) {
                    arrow.clearAnimation();
                    arrow.startAnimation(up);
                    rotated = true;
                }
            } else if (y < mHeaderHeight) {
                if (rotated) {
                    arrow.clearAnimation();
                    arrow.startAnimation(down);
                    rotated = false;
                }

                info.setText("向下拉动可以刷新");
            }
        }
    }

    @Override
    public void onRelease() {

    }

    /**
     * 加载完成
     */
    @Override
    public void onComplete() {
        rotated=false;
        arrow.clearAnimation();
        arrow.setVisibility(View.VISIBLE);
        pb.setVisibility(View.INVISIBLE);
        info.setText("加载完成");
    }

    @Override
    public void onReset() {
        pb.setVisibility(View.GONE);
        info.setVisibility(View.VISIBLE);
    }
}
